package com.dytj.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dytj.pojo.User;
import com.dytj.service.UserServiceImpl;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public UserServiceImpl userDao = new UserServiceImpl();
    public UserServlet() {
        super();
    }
    //    登录

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User user = new User();
		String accounts = request.getParameter("accounts");
		user.setAccount(accounts);
		String password = request.getParameter("password");
		user.setPassword(password);
		List<User> list = new ArrayList<User>();
		
		try {
			list = userDao.userCheck(user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(list.toString());
		if(!list.isEmpty()) {
			User value = new User();
			value.setAccount(list.get(0).getAccount());
			value.setGender(list.get(0).getGender());
			value.setNickName(list.get(0).getNickName());
			value.setPassword(list.get(0).getPassword());
			value.setUserId(list.get(0).getUserId());
			request.getSession(true).setAttribute("user",value);
		}else {
			String info = "用户名或密码不正确！";
			request.setAttribute("outputMessage", info);
			request.getRequestDispatcher("/WEB-INF/jsp/info.jsp").forward(request,response);
		}
	}

}
